package com.ms.game.sendgift.db;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ms.game.itembox.Item;
import com.ms.game.itembox.message.SSimpleItem;
import com.ms.game.sendgift.message.SendGiftNotifyMessage;
import com.ms.game.sendgift.message.SendGiftResponse;
import com.ms.service.error.ErrorCode;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.message.MessageTypes;
import com.xpec.c4.service.message.MessageAction;
import com.xpec.c4.service.module.common.PlayerCommonInfoManager;
import com.xpec.c4.service.module.task.TaskAsyncCommand;

public class SaveSendGiftNotifyDataAsync extends TaskAsyncCommand {

	private static Log log = LogFactory.getLog(SaveSendGiftNotifyDataAsync.class);
	
	/**
	 * MessageTypes.S_GIFT_FIRST_SEND_RESPONSE
	 */
	public int messageType;
	
	public SendGiftNotifyModel model;
	
	/**
	 * 寄送的道具列表
	 */
	public ArrayList<Item> sendGiftItemList;
	
	/**
	 * 玩家現在的金錢
	 */
	public BigDecimal newValue; 
	
	public GUID sessionID;
	
	public String playerIP;
	
	public String targetIP;
	
	private boolean isSaveDone = false;
	
	private long createTime;
	
	@Override
	public void asyncExecute() throws Exception {
		
		if(SendGiftDAO.saveOrUpdateGiftNotify(model)){
			isSaveDone = true;
		}

		//log 記錄
		if(isSaveDone){
			SendGiftRecordLog logModel = new SendGiftRecordLog();
			logModel.setOrderID(model.getOrderID());
			logModel.setSendOrderID(model.getSendOrderID());
			logModel.setOrderNumber(model.getOrderNumber());
			logModel.setPlayerGUID(model.getPlayerGUID());
			logModel.setPlayerName(model.getPlayerName());
			logModel.setPlayerIP(playerIP);
			logModel.setTargetGUID(model.getTargetGUID());
			logModel.setTargetName(model.getTargetName());
			logModel.setTargetIP(targetIP);
			logModel.setGoldCoin(model.getGoldCoin());
			logModel.setServiceCharge(model.getServiceCharge());
			logModel.setItems(model.getItems());
			logModel.setStage(model.getStage());
			logModel.setCreateTime(model.getCreateTime());
			logModel.setAnswerTime(model.getAnswerTime());
			logModel.setFinalTakenTime(model.getFinalTakenTime());
			logModel.setCompleteTime(model.getFinishTime());
			logModel.setOverTime(model.getOverTime());
			logModel.setSuccess(false);
			logModel.setFinish(model.isFinish());
			logModel.setTaken(model.isTaken());	
			
			createTime = model.getCreateTime().getTime();
			
			SendGiftRecordDAO.saveOrUpdateGiftNotify(logModel);
		}
	}

	/**
	 * @see com.ms.game.sendgift.SendGiftProcessor#OnSendGiftFirstStepCallBack
	 */
	@Override
	public void execute() throws Exception {
		
		SendGiftResponse response = new SendGiftResponse();
		if(!isSaveDone){
			response.errorCode = ErrorCode.SEND_GIFT_SAVE_ERROR;
			MessageAction.sendMessage(sessionID, messageType, response);
			return;
		}

		// 寄送的道具資料
		HashMap<String , SSimpleItem> sendItemMap = new HashMap<String , SSimpleItem>();
		for(Item item : sendGiftItemList){
			sendItemMap.put(item.getSn(), item.toClientFormat());
		}

		//正確回傳玩家
		response.orderID = model.getSendOrderID();
		response.orderNumber = model.getOrderNumber();
		response.goldCoin = model.getGoldCoin(); //回傳寄送多少錢
		response.charge = model.getServiceCharge(); //手續費
		response.nowGoldCoin = newValue.toString();
		response.items = sendItemMap;
		response.createTime = createTime;
		response.errorCode = ErrorCode.SUCCESS;
		MessageAction.sendMessage(sessionID, messageType, response);
				
		//給目標通知
		GUID targetSessionID = PlayerCommonInfoManager.getInstance().getSessionByPlayerGuid(GUID.parseUUID(model.getTargetGUID()));
		SendGiftNotifyMessage notifyResponse = new SendGiftNotifyMessage();
		//在線上的話要告知
		if(targetSessionID != null){
			notifyResponse.orderID = model.getSendOrderID();
			notifyResponse.fromName = model.getPlayerName(); //名子
			notifyResponse.goldCoin = model.getGoldCoin(); //寄送多少錢
			notifyResponse.createTime = createTime;
			notifyResponse.items = sendItemMap;
			MessageAction.sendMessage(targetSessionID, MessageTypes.S_GIFT_FIRST_NOTIFY_TARGET_RESPONSE, notifyResponse);
		}
	}

}
